// 抽奖系统
var webapi = "https://api-shake.xinglongdayuan.com";
var webApp = {};
var mid = "DJEHBSUAAY";
var idmum;
var timestamp; // 当前时间戳
var startTime = 1552014000; //起始时间
var interval = 60; //间隔时长，单位是分钟
var number = 13; //活动轮数
var intDiff = parseInt(119); //倒计时总秒数量
var intDiff1 = parseInt(59); //倒计时总秒数量1
var dTimer = null;
var dTimer1 = null;
var sTimer0 = null;
var times;
webApp.getTime = webapi + "/ol/shake/getTime/" + mid; //获取id
webApp.shake_detail = webapi + "/ol/shake/getShakeId/" + mid; //获取id
webApp.lucky_list = webapi + "/ol/shake/allLogData/" + mid + "?"; //中奖名单

gettime();
// 获取中奖名单
function getId() {
    $.ajax({
        url: webApp.shake_detail,
        type: "POST",
        dataType: 'json',
        // data: {ids},
        success: function (data) {
            if (data.Error == 0) {
                console.log(data.data.id);
                idmum = data.data.id; //获取id
                // getList();
            } else {
                mui.toast(data.Msg);
            }

        }
    });
}

function getList() {
    var html1 = "";
    $.ajax({
        // url: webApp.lucky_list + "id=" + idmum,
        url: webApp.lucky_list + "id=131",
        type: "POST",
        dataType: 'json',
        // data: {ids},
        success: function (data) {
            if (data.Error == 0) {
                // console.log(data.data);

                for (var i = 0; i < data.data.length; i++) {
                    // var _phone = data.data[i].phone;
                    // console.log(_phone)
                    // var mphone = _phone.substring(0, 3) + '****' + _phone.substring(7);
                    html1 += '<div class="z_user">' + data.data[i].prize + '</div>';
                }
            } else {
                mui.toast(data.Msg);
            }
            $("#d_top").html("");
            $("#d_top").append(html1);
        }
    });
}

// 判断是否有id
function getData() {
    if (idmum != null) {
        // getList();
    } else {
        // getId();
    }
}
// 中奖名单滚动
$(function () {
    getListlenghtTtimer = setInterval(getListTtimer, 1000);

    function getListTtimer() {
        if ($(".z_user").length > 0) {
            clearInterval(getListlenghtTtimer);
            var prizeLenght = $(".z_user").length;
            var divHeight = prizeLenght * 46.39;
            clearInterval(scrollTimer);
            var oTop = 0;
            var scrollTimer = null;
            scrollTimer = setInterval(scrollTime, 50);

            function scrollTime() {
                if (oTop > divHeight) {
                    // alert(123)
                    oTop = 0;
                    $("#d_top").css({
                        top: "0px"
                    });
                } else if (oTop < divHeight) {
                    $("#d_top").css({
                        top: "-" + oTop + "px"
                    });
                    oTop += 1;
                    // console.log("oTop---" + oTop)
                    // console.log("divHeight---" + divHeight)
                }
            }
        }
    }
})

// 奖池倒计时
var sTimer = null;

function timer() {
    var minute = 0,
        second = 0; //时间默认值        
    if (intDiff > 0) {
        day = Math.floor(intDiff / (60 * 60 * 24));
        hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
        minute = Math.floor(intDiff / 60) - (hour * 60);
        second = Math.floor(intDiff) - (minute * 60) - (hour * 3600);
    }
    if (minute <= 9) minute = '0' + minute;
    if (second <= 9) second = '0' + second;
    // $('#day_show').html(day+"天");
    $('#hour_show').html('<s></s>' + hour + '时');
    $('#minute_show').html('<s></s>' + minute + ' 分');
    $('#second_show').html('<s></s>' + second + ' 秒');
    // 活动未开始的时间
    $('#uhour_show').html('<s></s>' + hour + '时');
    $('#uminute_show').html('<s></s>' + minute + ' 分');
    $('#usecond_show').html('<s></s>' + second + ' 秒');
    intDiff--;
}

function timer0() {
    var minute = 0,
        second = 0; //时间默认值        
    if (intDiff1 > 0) {
        day = Math.floor(intDiff1 / (60 * 60 * 24));
        hour = Math.floor(intDiff1 / (60 * 60)) - (day * 24);
        minute = Math.floor(intDiff1 / 60) - (hour * 60);
        second = Math.floor(intDiff1) - (minute * 60) - (hour * 3600);
    }
    if (minute <= 9) minute = '0' + minute;
    if (second <= 9) second = '0' + second;
    // $('#day_show').html(day+"天");
    $('#hour_show').html('<s></s>' + hour + '时');
    $('#minute_show1').html('<s></s>' + minute + ' 分');
    $('#second_show1').html('<s></s>' + second + ' 秒');
    intDiff1--;
}

// 获取当前时间
function getNow(s) {
    return s < 10 ? '0' + s : s;
}

function gettime() {
    $.ajax({
        url: webApp.getTime,
        type: "POST",
        dataType: 'json',
        // data: {ids},
        success: function (data) {
            if (data.Error == 0) {
                console.log(data.time);
                timestamp = data.time;
                // 页面刷新判断当前times
                if (startTime < timestamp) {
                    // getId();
                    times = parseInt((timestamp - startTime) / (interval * 60)) + 1;
                } else {
                    times = 1;
                    $(".cen-right1 h3").hide();
                    $("#ustart-time").show();
                }
                clearInterval(dTimer1);
                dTimer1 = setInterval(dTime1, 1000);
            } else {
                mui.toast(data.Msg);
            }

        }
    });
}

function add0(m) {
    return m < 10 ? '0' + m : m
}

function format(shijianchuo) {
    //shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo * 1000);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    // var startTime = 1550210518 //起始时间
    // var number = 10 //活动轮数
    // var interval = 5 //间隔时长，单位是分钟
    return add0(h) + ':' + add0(mm) + ':' + add0(s);
}

function dTime1() {
    var nowTime1 = format(timestamp);
    $("#d_time").html(nowTime1);
    timestamp++
    //活动未开始等待时间
    if (timestamp < startTime) {
        $(".cen-right1").css("display", "block");
        $(".cen-right").css("display", "none");
        intDiff = parseInt(startTime - timestamp);
        clearInterval(sTimer);
        sTimer = setInterval(timer(), 1000);
    } else if (timestamp == startTime) {
        $("#ustart-time").hide();
        $(".cen-right1 h3").show();
    } else {
        //活动进行中
        if (timestamp > startTime && timestamp < startTime + interval * 60 * number) {
            //4分钟等待
            if (timestamp < startTime + (times - 1) * interval * 60 + 60 * interval && startTime + (times - 1) * interval * 60 + 60 < timestamp) {
                $("#firstRound" + times).css("color", "#fbe300").siblings().css("color", "#fff");
                $("#firstRound" + times).css("fontSize", "1rem").siblings().css("fontSize", "0.8rem");
                // $(".cen-left img").attr("src", "images/2019.03.08/pc-chj/" + times + ".jpg");
                $(".cen-right").css("display", "block");
                $(".cen-right1").css("display", "none");
                intDiff = parseInt(startTime + (times - 1) * interval * 60 + 60 * interval - timestamp);
                clearInterval(sTimer);
                sTimer = setInterval(timer(), 1000);
                // getList();
                if (times == number) {
                    clearInterval(dTimer1);
                    $(".cen-right1").hide();
                    $(".cen-right").hide();
                    $(".hd-over").show();
                }
            }
            //1分钟摇奖时间
            if (timestamp < startTime + (times - 1) * interval * 60 + 60) {
                $("#firstRound" + times).css("color", "#fbe300").siblings().css("color", "#fff");
                $("#firstRound" + times).css("fontSize", "1rem").siblings().css("fontSize", "0.8rem");
                // $(".cen-left img").attr("src", "images/2019.03.08/pc-chj/" + times + ".jpg");
                $(".cen-right1").css("display", "block");
                $(".cen-right").css("display", "none");
                intDiff1 = parseInt(startTime + (times - 1) * interval * 60 + 60 - timestamp);
                clearInterval(sTimer0);
                sTimer0 = setInterval(timer0(), 1000);
                // getData();
            }
            //到时间进行下一轮
            if (timestamp == startTime + times * interval * 60) {
                idmum = null;
                times++;
                // getData();
            }
        } else {
            clearInterval(dTimer1);
            $(".cen-right1").hide();
            $(".cen-right").hide();
            $(".hd-over").show();
        }

    }
}

// 刷新页面
document.addEventListener('visibilitychange', function () {
    if (document.visibilityState == 'visible') {
        location.reload();
    }
})